package com.qc31.monitorlib.ui.video.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.qc31.monitorlib.R
import com.qc31.monitorlib.ui.video.CustomVideoManager
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import com.shuyu.gsyvideoplayer.video.base.GSYVideoViewBridge

/**
 *@Author Jasper
 *@Time   2020/10/15
 *@Desc   平台视频
 */
class PlatFormVideo : StandardGSYVideoPlayer {
    private lateinit var ivSwitchVoice: ImageView
    private lateinit var tvSwitchScale: TextView

    lateinit var startPlay: ImageView
    //是否需要静音
    private var isNeedMute = true

    //记住切换数据源类型
    private var mScaleType = 0

    constructor(context: Context?, fullFlag: Boolean?) : super(context, fullFlag)
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    override fun getLayoutId(): Int {
        return R.layout.layout_platform_video
    }

    override fun init(context: Context?) {
        super.init(context)
        startPlay = findViewById(R.id.startPlay)
        isRotateViewAuto = true //是否开启制动旋转
        isShowFullAnimation = true
        isReleaseWhenLossAudio = false
        setThumbPlay(false)
        setIsTouchWiget(false)//禁止滑动
        setIsTouchWigetFull(false)//禁止滑动改变声音
        isLockLand = true
        CustomVideoManager.instance.isNeedMute = true
        ivSwitchVoice = findViewById(R.id.ivSwitchVoice)
        tvSwitchScale = findViewById(R.id.tvSwitchScale)
        ivSwitchVoice.isSelected = true
        setListener()
        setBarEnable()
    }

    private fun setListener() {
        //切换比例
        tvSwitchScale.setOnClickListener {
            if (!mHadPlay) {
                return@setOnClickListener
            }
            when (mScaleType) {
                0 -> {
                    mScaleType = 1
                }
                1 -> {
                    mScaleType = 2
                }
                2 -> {
                    mScaleType = 3
                }
                3 -> {
                    mScaleType = 0
                }
            }
            resolveTypeUI()
        }
        //切换声音
        ivSwitchVoice.setOnClickListener {
            if (!mHadPlay) {
                return@setOnClickListener
            }
            isNeedMute = !isNeedMute
            ivSwitchVoice.isSelected = isNeedMute
            CustomVideoManager.instance.isNeedMute = isNeedMute

        }
    }

    /**
     * 设置播放按钮图标
     */
    override fun updateStartImage() {
        when (mCurrentState) {
            GSYVideoView.CURRENT_STATE_PLAYING -> {
                startPlay.setImageResource(R.mipmap.video_pause_pressed)
            }
            GSYVideoView.CURRENT_STATE_ERROR -> {
                startPlay.setImageResource(R.mipmap.video_play_normal)
            }
            else -> {
                startPlay.setImageResource(R.mipmap.video_play_normal)
            }
        }
    }

    override fun startWindowFullscreen(
        context: Context?,
        actionBar: Boolean,
        statusBar: Boolean
    ): GSYBaseVideoPlayer {
        val video: PlatFormVideo =
            super.startWindowFullscreen(context, actionBar, statusBar) as PlatFormVideo
        video.mScaleType = mScaleType
        video.isNeedMute = isNeedMute
        video.resolveTypeUI()
        video.resolveVoice()
        return video
    }

    override fun resolveNormalVideoShow(
        oldF: View?,
        vp: ViewGroup?,
        gsyVideoPlayer: GSYVideoPlayer?
    ) {
        super.resolveNormalVideoShow(oldF, vp, gsyVideoPlayer)
        gsyVideoPlayer?.let {
            val video: PlatFormVideo = it as PlatFormVideo;
            isNeedMute = video.isNeedMute
            mScaleType = video.mScaleType
            resolveVoice()
            resolveTypeUI()
        }

    }

    override fun getGSYVideoManager(): GSYVideoViewBridge? {
        return CustomVideoManager.instance
    }

    override fun releaseVideos() {
        CustomVideoManager.instance.releaseMediaPlayer()
    }

    override fun touchDoubleUp(e: MotionEvent?) {

    }

    override fun touchSurfaceMove(deltaX: Float, deltaY: Float, y: Float) {}

    /**
     * 显示比例
     * 注意，GSYVideoType.setShowType是全局静态生效，除非重启APP。
     */
    private fun resolveTypeUI() {
        if (!mHadPlay) {
            return
        }
        when (mScaleType) {
            1 -> {
                tvSwitchScale.text = "16:9"
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_16_9)
            }
            2 -> {
                tvSwitchScale.text = "4:3"
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_4_3)
            }
            3 -> {
                tvSwitchScale.setText(R.string.desc_video_full_screen)
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL)
            }
            0 -> {
                tvSwitchScale.setText(R.string.desc_video_default_ratio)
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT)
            }
        }
        changeTextureViewShowType()
        if (mTextureView != null) mTextureView.requestLayout()
    }


    /**
     * 设置声音UI
     */
    private fun resolveVoice() {
        if (!mHadPlay) {
            return
        }
        ivSwitchVoice.isSelected = !isNeedMute
    }

    var durationT: Long = 10

    /**
     * 设置进度条最大值
     */
    override fun getDuration(): Long {
        return durationT
    }

    /**
     * 进度条不可点击
     */
    fun setBarEnable() {
        mProgressBar.isClickable = false;
        mProgressBar.isEnabled = false;
        mProgressBar.isFocusable = false;
    }
    override fun setStateAndUi(state: Int) {
        super.setStateAndUi(state)
        when (state) {
            GSYVideoView.CURRENT_STATE_PLAYING -> {
                startPlay.visibility = View.GONE
            }
        }
    }
}